package com.wonderfulmall.cart.interceptor;

import com.wonderful.common.constant.AuthServerConstant;
import com.wonderful.common.constant.CartConstant;
import com.wonderful.common.vo.MemberRespVo;
import com.wonderfulmall.cart.vo.UserInfoTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * 在执行方法前判断用户的登录状态
 */
public class CartInterceptor implements HandlerInterceptor {
    public static ThreadLocal<UserInfoTo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserInfoTo userInfoTo = new UserInfoTo();
        HttpSession session = request.getSession();
        MemberRespVo member = (MemberRespVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (member != null) {
            userInfoTo.setUserId(member.getId());
            userInfoTo.setTempUser(false);
            Cookie[] cookies = request.getCookies();
            if (cookies != null && cookies.length > 0) {
                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)) {
                        userInfoTo.setUserKey(cookie.getValue());
                    }
                }
            }
        } else {
            Cookie[] cookies = request.getCookies();
            if (cookies != null && cookies.length > 0) {
                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)) {
                        userInfoTo.setUserKey(cookie.getValue());
                    }
                }
                if (StringUtils.isEmpty(userInfoTo.getUserKey())) {
                    userInfoTo.setUserKey(UUID.randomUUID().toString());
                    userInfoTo.setTempUser(true);
                }
            }
        }
        threadLocal.set(userInfoTo);
        return true;
    }

    /**
     * 业务执行之后
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        UserInfoTo userInfoTo = threadLocal.get();
        // 为临时用户设置浏览器端的 cookie
        Cookie cookie;
        if (userInfoTo.isTempUser()) {
            cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfoTo.getUserKey());

        }else{
             cookie = new Cookie(CartConstant.USER_COOKIE_NAME, String.valueOf(userInfoTo.getUserId()));
        }
        cookie.setDomain("wonderfulmall.com");
        cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
        response.addCookie(cookie);
    }
}
